package com.akeil.flash.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.akeil.flash.pojo.Data;
import com.akeil.flash.service.IDataService;
import com.akeil.flash.utils.DataStatisticsUtil;
import com.akeil.flash.utils.ExcelUtil;

import flash.Activator;
import flash.Application;

public class OutputExcelAction extends AbstractFlashAction {
	private static Logger logger = Logger.getLogger(OutputExcelAction.class);
	IDataService dataService = Application.applicationContext
			.getBean(IDataService.class);
	private TableViewer tableViewer;

	public OutputExcelAction(TableViewer tableViewer) {
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(
				Activator.PLUGIN_ID, "icons/history/excel_16px.png"));
		this.setToolTipText(resourceBundle.getString("toolbar.output.excel.tooltip"));
		this.setText(resourceBundle.getString("toolbar.output.excel.name"));
		this.tableViewer = tableViewer;
	}

	@Override
	public void run() {
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		FileDialog fileDialog = new FileDialog(parentShell,SWT.SAVE);
		fileDialog.setFilterExtensions(new String[]{"*.xls"});
		String filename ;
		filename = fileDialog.open();
		if(filename != null){
			List<Data> datas = (List<Data>) tableViewer.getInput();
			
			ExcelUtil.createExcel(DataStatisticsUtil.processIndex(datas), filename);
		}
		super.run();
	}
}
